package com.wzzc.servlet;

import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.net.GuacamoleSocket;
import org.apache.guacamole.net.GuacamoleTunnel;
import org.apache.guacamole.net.InetGuacamoleSocket;
import org.apache.guacamole.net.SimpleGuacamoleTunnel;
import org.apache.guacamole.protocol.ConfiguredGuacamoleSocket;
import org.apache.guacamole.protocol.GuacamoleConfiguration;
import org.apache.guacamole.servlet.GuacamoleHTTPTunnelServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;

import com.wzzc.bean.Student;
import com.wzzc.controller.UserController;
import com.wzzc.service.UserService;
import com.wzzc.utils.DockerUtils;
import com.wzzc.utils.MyUtils;

public class GuacamoleTunnelServlet extends GuacamoleHTTPTunnelServlet {
	
	private UserService userService;
	
	@Override
	public void init() throws ServletException {
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		userService=(UserService) applicationContext.getBean("userService");
	}

	@Override
	protected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException {
        Student user = (Student) request.getSession().getAttribute(UserController.SESSION_USER);
        if (user!=null) {
        	String name=user.getStudentNo();
    		DockerUtils.openDocker(name);
    		String dockerip = DockerUtils.getDockerIp(name);
    		GuacamoleConfiguration config = new GuacamoleConfiguration();
    		config.setProtocol("vnc");
    		config.setParameter("hostname", dockerip);
    		config.setParameter("port", "5901");
    		config.setParameter("password", "123456");
    		GuacamoleSocket socket = new ConfiguredGuacamoleSocket(new InetGuacamoleSocket("localhost", 4822), config);
    		System.out.println(name+"连接到服务器,DockerIP:"+dockerip);
    		SimpleGuacamoleTunnel simpleGuacamoleTunnel = new SimpleGuacamoleTunnel(socket);
    		String uiid = simpleGuacamoleTunnel.getUUID().toString();
    		System.out.println(userService);
    		userService.updateStudentUIID(user.getId(), uiid);
    		MyUtils.dockerImagesMap.put(uiid, name);
    		return simpleGuacamoleTunnel;
		}
		return null;
	}
	
	@Override
	protected void deregisterTunnel(GuacamoleTunnel tunnel) {
		super.deregisterTunnel(tunnel);
		String uiid=tunnel.getUUID().toString(); 
		Student student=new Student();
		student.setDockerStarttime(new Date().toString());
		student.setDockerState(0);
		userService.updateStudentDockerState(uiid,student);
		if (MyUtils.dockerImagesMap.containsKey(uiid)) {
			try {
				String name=MyUtils.dockerImagesMap.get(uiid);
				MyUtils.dockerImagesMap.remove(uiid);
				DockerUtils.closeDocker(name);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println("End: "+tunnel.getSocket().getReader());
	}

	@Override
	protected void registerTunnel(GuacamoleTunnel tunnel) {
		super.registerTunnel(tunnel);
		String uiid=tunnel.getUUID().toString();
		Student student=new Student();
		student.setDockerState(1);
		userService.updateStudentDockerState(uiid,student);
		System.out.println("Start: "+tunnel.getSocket().getReader());
	}
	
}